<?php

class Materials_Model_Material_Observer_CategoryStatusUpdater extends Workset_Model_Observer_Abstract {

    public function afterSave(Workset_Model_Subject_Interface $object, $data = null) {

        if ($object instanceof Materials_Model_Material) {
            $categories = $object->getCategories();

            foreach ($categories as $category) {
                $count = (int)$category->materialsCount(array('m.status = ?' => Materials_Model_Material::STATUS_PUBLISHED));

                if ($count > 0) {
                    continue;
                }

                $category['status'] = Materials_Model_Material::STATUS_DRAFT;
                $category->save();
            }

        }

    }

}